package com.hms.entity;

import java.util.Collection;
import java.util.Date;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * 管理员实体类<br>
 * 类详细说明.
 * <p>
 * Copyright: Copyright (c) 2013-3-29 下午1:59:32
 * <p>
 * Company: 北京宽连十方数字技术有限公司
 * <p>
 * 
 * @author wangpz@c-platform.com
 * @version 1.0.0
 */
@Entity
@Table(name = "td_admin")
public class Admin extends BaseEntity implements UserDetails {

	private static final long serialVersionUID = -7519486823153844426L;

	private String username;// 用户名

	private String password;// 密码

	private String email;// E-mail

	private String name;// 姓名

	private String department;// 部门

	private Boolean isAccountEnabled;// 账号是否启用

	private Boolean isAccountLocked;// 账号是否锁定

	private Boolean isAccountExpired;// 账号是否过期

	private Boolean isCredentialsExpired;// 凭证是否过期

	private Integer loginFailureCount;// 连续登录失败的次数

	private Date lockedDate;// 账号锁定日期

	private Date loginDate;// 最后登录日期

	private String loginIp;// 最后登录IP

	private Set<Role> roleSet;// 管理角色

	private Collection<GrantedAuthority> authorities;;// 角色信息

	@Override
	@Column(updatable = false, nullable = false, unique = true)
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Override
	@Column(nullable = false)
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Column(nullable = false)
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	@Column(nullable = false)
	public Boolean getIsAccountEnabled() {
		return isAccountEnabled;
	}

	public void setIsAccountEnabled(Boolean isAccountEnabled) {
		this.isAccountEnabled = isAccountEnabled;
	}

	@Column(nullable = false)
	public Boolean getIsAccountLocked() {
		return isAccountLocked;
	}

	public void setIsAccountLocked(Boolean isAccountLocked) {
		this.isAccountLocked = isAccountLocked;
	}

	@Column(nullable = false)
	public Boolean getIsAccountExpired() {
		return isAccountExpired;
	}

	public void setIsAccountExpired(Boolean isAccountExpired) {
		this.isAccountExpired = isAccountExpired;
	}

	@Column(nullable = false)
	public Boolean getIsCredentialsExpired() {
		return isCredentialsExpired;
	}

	public void setIsCredentialsExpired(Boolean isCredentialsExpired) {
		this.isCredentialsExpired = isCredentialsExpired;
	}

	@Column(nullable = false)
	public Integer getLoginFailureCount() {
		return loginFailureCount;
	}

	public void setLoginFailureCount(Integer loginFailureCount) {
		this.loginFailureCount = loginFailureCount;
	}

	public Date getLockedDate() {
		return lockedDate;
	}

	public void setLockedDate(Date lockedDate) {
		this.lockedDate = lockedDate;
	}

	@Column(nullable = false)
	public Date getLoginDate() {
		return loginDate;
	}

	public void setLoginDate(Date loginDate) {
		this.loginDate = loginDate;
	}

	@Column(nullable = false)
	public String getLoginIp() {
		return loginIp;
	}

	public void setLoginIp(String loginIp) {
		this.loginIp = loginIp;
	}

	@ManyToMany(fetch = FetchType.EAGER)
	@JoinColumn(nullable = false)
	@OrderBy("name asc")
	public Set<Role> getRoleSet() {
		return roleSet;
	}

	public void setRoleSet(Set<Role> roleSet) {
		this.roleSet = roleSet;
	}

	@Override
	@Transient
	public Collection<GrantedAuthority> getAuthorities() {
		return authorities;
	}

	public void setAuthorities(Collection<GrantedAuthority> authorities) {
		this.authorities = authorities;
	}

	@Override
	@Transient
	public boolean isEnabled() {
		return this.isAccountEnabled;
	}

	@Override
	@Transient
	public boolean isAccountNonLocked() {
		return !this.isAccountLocked;
	}

	@Override
	@Transient
	public boolean isAccountNonExpired() {
		return !this.isAccountExpired;
	}

	@Override
	@Transient
	public boolean isCredentialsNonExpired() {
		return !this.isCredentialsExpired;
	}

}
